package com.ProdigiousAT;

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.Select;

/**
 * Created by Worker on 11/01/2015.
 */
public class KW {

    public static void enterText(WebDriver driver, String xPath, String data){
        //		Purpose: Enter Text into a edit field
        //		I/P:driver, xPath, Data
        //		O/P:Nil
        //		CreatedBy:SelA16 Batch
        //		When:9/11/12
        //		EditedBy:Karthik
        //		When:9/11/12
        driver.findElement(By.xpath(xPath)).clear();
        driver.findElement(By.xpath(xPath)).sendKeys(data);
    }

    public static String readText(WebDriver driver, String xPath){
        //		Purpose: Read Text from a edit field
        //		I/P:driver, xPath
        //		O/P:Data
        //		CreatedBy:SelA16 Batch
        //		When:9/11/12
        //		EditedBy:Karthik
        //		When:9/11/12
        return driver.findElement(By.xpath(xPath)).getText();
    }

    public static String verifyText(WebDriver driver, String xPath, String sText){
        //		Purpose: Verify Text from a edit field
        //		I/P:driver, xPath
        //		O/P:Data
        //		CreatedBy:SelA16 Batch
        //		When:9/11/12
        //		EditedBy:Karthik
        //		When:9/11/12
        if (sText.equalsIgnoreCase(driver.findElement(By.xpath(xPath)).getText())){
            return "Pass";
        }else {
            return "Fail";
        }

    }

    public static void clickElement(WebDriver driver, String xPath){
        //		Purpose: Click any element on the AUT
        //		I/P:driver, xPath
        //		O/P:Nil
        //		CreatedBy:SelA16 Batch
        //		When:9/11/12
        //		EditedBy:Karthik
        //		When:9/11/12
        //driver.findElement(By.xpath(xPath)).click();
        driver.findElement(By.xpath(xPath)).sendKeys("\n");
    }

    public static void selectList(WebDriver driver, String xPath, String data){
        //		Purpose: Select from a dropdown list
        //		I/P:driver, xPath, data
        //		O/P:Nil
        //		CreatedBy:SelA16 Batch
        //		When:9/11/12
        //		EditedBy:Karthik
        //		When:9/11/12
        Select myDD = new Select(driver.findElement(By.xpath(xPath)));
        myDD.selectByVisibleText(data);
        myDD = null;
    }

    public static void checkBox(WebDriver driver, String xPath){
        //		Purpose: Check a checkbox
        //		I/P:driver, xPath, data
        //		O/P:Nil
        //		CreatedBy:SelA16 Batch
        //		When:9/11/12
        //		EditedBy:Karthik
        //		When:9/11/12
        // Is it checked
        if(driver.findElement(By.xpath(xPath)).getAttribute("checked").equalsIgnoreCase("checked")){
            //Then dont do anything
        } else {
            driver.findElement(By.xpath(xPath)).click();
        }
    }

    public static void navigateBrowser(WebDriver driver, String data){
        //		Purpose: Navigate a browser to a URL
        //		I/P:driver, data
        //		O/P:Nil
        //		CreatedBy:SelA16 Batch
        //		When:9/11/12
        //		EditedBy:Karthik
        //		When:9/11/12
        // Is it checked
        driver.navigate().to(data);
    }

    public static void waitTime(long lTime) throws Exception{
        //		Purpose: Waits certain amount of time
        //		I/P:driver, data
        //		O/P:Nil
        //		CreatedBy:SelA16 Batch
        //		When:9/11/12
        //		EditedBy:Karthik
        //		When:9/11/12
        // Is it checked
        Thread.sleep(lTime);
    }

    public static String waitForElement(WebDriver driver, String xPath) throws Exception{
        for (int second = 0;; second++) {
            if (second < 20){
                if (isElementPresent(driver, By.xpath(xPath))){
                    return "Pass";
                }
            } else {
                return "Fail";
            }
            Thread.sleep(1000);
        }
    }

    private static boolean isElementPresent(WebDriver driver, By by) {
        try {
            driver.findElement(by);
            return true;
        } catch (NoSuchElementException e) {
            return false;
        }
    }
}
